package com.kehao.leetcode.basic.tree;


public class TreeSolution5 {
    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public TreeNode sortedArrayToBST(int[] nums) {
        int mid = (nums.length-1)/2;
        TreeNode root = new TreeNode(nums[mid]);
        root.left = sortedArrayToBST(nums,0,mid-1);
        root.right = sortedArrayToBST(nums,mid+1,nums.length-1);
        return root;
    }

    //包含start和end
    public TreeNode sortedArrayToBST(int[] nums,int start,int end) {
        if(end<start) return null;
        int mid = (start+end)/2;
        TreeNode root = new TreeNode(nums[mid]);
        root.left = sortedArrayToBST(nums,start,mid-1);
        root.right = sortedArrayToBST(nums,mid+1,end);
        return root;
    }

}
